﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace HangerSupport
{
	public class SelectLines
	{
		public SelectLines(Autodesk.Revit.DB.Document doc, Selection sel)
		{
			this.Doc = doc;
			this.Sel = sel;
		}

		public List<MEPCurve> GetMEPCurves(ISelectionFilter selectfilter)
		{
			List<MEPCurve> list;
			for (;;)
			{
				list = this.Sel.PickElementsByRectangle(selectfilter, "请框选需要计算的管线").Cast<MEPCurve>().ToList<MEPCurve>();
				bool flag = list != null && list.Count > 0;
				if (flag)
				{
					break;
				}
				YJKMessageBox.Information("未框选到需要计算的管线！");
			}
			return list;
		}

		public List<SystemLine> GetSystemLines(List<MEPCurve> list)
		{
			List<SystemLine> list2 = new List<SystemLine>();
			foreach (MEPCurve line in list)
			{
				bool flag = this.IsNoExistPipe(line, list2);
				if (flag)
				{
					list2.Add(this.getSystemLine(line));
				}
			}
			return list2;
		}

		private SystemLine getSystemLine(MEPCurve line)
		{
			SystemLine systemLine = new SystemLine();
			Parameter parameter = line.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM);
			bool flag = parameter != null;
			if (flag)
			{
				bool flag2 = parameter.AsString() == "" || parameter.AsString() == null;
				if (flag2)
				{
					parameter = line.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_SYSTEM_NAME_PARAM);
				}
				systemLine.SystemName = parameter.AsString();
			}
			else
			{
				systemLine.SystemName = "1";
			}
			systemLine.SymbolName = line.Name;
			List<MEPCurve> pipes = new List<MEPCurve>();
			ElementNode elemNode = new ElementNode(line);
			SearchBranch searchBranch = new SearchBranch(elemNode);
			searchBranch.DoSearch();
			YJKLine line2 = new YJKLine((Line)((LocationCurve)line.Location).Curve);
			this.getOnLinePipe(line2, searchBranch.GetListRoot(), ref pipes);
			systemLine.Pipes = pipes;
			return systemLine;
		}

		private bool IsNoExistPipe(MEPCurve line, List<SystemLine> res)
		{
			bool flag = res != null && res.Count > 0;
			if (flag)
			{
				List<MEPCurve> list = new List<MEPCurve>();
				foreach (SystemLine systemLine in res)
				{
					list.AddRange(systemLine.Pipes);
				}
				bool flag2 = (from s in list
				where s.Id == line.Id
				select s).Any<MEPCurve>();
				if (flag2)
				{
					return false;
				}
			}
			return true;
		}

		private void getOnLinePipe(YJKLine line, ElementNode eNode, ref List<MEPCurve> list)
		{
			bool flag = eNode == null;
			if (!flag)
			{
				bool flag2 = eNode.Element is MEPCurve;
				if (flag2)
				{
					bool flag3 = this.isOnline(line, eNode);
					if (flag3)
					{
						list.Add((MEPCurve)eNode.Element);
					}
				}
				bool flag4 = eNode.Next != null;
				if (flag4)
				{
					this.getOnLinePipe(line, eNode.Next, ref list);
				}
				bool flag5 = eNode.SubElementNode != null;
				if (flag5)
				{
					foreach (ElementNode eNode2 in eNode.SubElementNode)
					{
						this.getOnLinePipe(line, eNode2, ref list);
					}
				}
			}
		}

		private bool isOnline(YJKLine line, ElementNode eNode)
		{
			return line.IsLineOnLine((Line)((LocationCurve)((MEPCurve)eNode.Element).Location).Curve);
		}

		private Document Doc = null;

		private Selection Sel = null;
	}
}
